<?php

namespace app\api\validate;

use app\common\model\User;
use think\facade\Log;
use think\Validate;

/**
 * 找回密码
 * @package app\api\validate
 */
class ResettingValidate extends Validate
{
    //定义验证规则
    protected $rule = [
        'type|步骤'       => 'require|between:1,3|sceneType',
        'account|账号'    => 'require|mobile|scenePhone',
        'password|密码'   => 'require|min:8|sceneReg|scenePassword',
        'passwordAver|确认密码'   => 'require|min:8',
        'code|短信验证码'  => 'require|min:6|sceneCode',
    ];

    //定义验证提示
    protected $message = [
        'account.require'       => '手机号不能为空',
        'account.mobile'        => '手机号错误',
        'account.scenePhone'    => '手机号未注册',
        'password.require'      => '密码不能为空',
        'password.sceneReg'     => '密码必须字母加数字',
        'password.scenePassword' => '新密码与确认密码不一致',
        'code.sceneCode'        => '短信验证码错误',
    ];


    /**
     *验证手机号
     */
    public function scenePhone($phone)
    {

        $where['username|mobile'] =  $phone;
        $token = User::where($where)->count();
        return (bool)$token;
    }

    /**
     *手机号找回验证码
     */
    public function sceneCode($code)
    {
        $account = request()->post('account');
        try {
            $redisCode = redis()->get('resettingCode_' . $account);
            redis()->del('resettingCode_' . $account);
            redis()->close();
        } catch (\RedisException $e) {
            Log::info('LoginCodeError_'.$e->getMessage());
        }
        return $code === $redisCode;
    }

    public function sceneType($type)
    {
        switch ($type){
            case 1:
                //步骤1只验证手机号
                $this->append('account', ['require','scenePhone'])
                      ->remove('password',['require','min','sceneReg','scenePassword'])
                      ->remove('passwordAver',['require','min'])
                      ->remove('code',['require','min','sceneCode']);
                return true;
            case 2:
                //步骤2只验证码
                $this->append('account', ['require','scenePhone'])
                     ->append('code',['require','min','sceneCode'])
                     ->remove('password',['require','min','sceneReg','scenePassword'])
                     ->remove('passwordAver',['require','min']);
                return true;
            case 3:
                //步骤3验证手机号密码
                $this->append('account', ['require','scenePhone'])
                     ->append('password',['require','min','sceneReg','scenePassword'])
                     ->append('passwordAver',['require','min']);
                return true;
            default:
                return false;
        }
    }

    public function scenePassword($password){
        $passwordAver = request()->post('passwordAver');
        return $password === $passwordAver;
    }

    public function sceneReg($password){
        $pattern = '/(?=.*[a-zA-Z])(?=.*[0-9])^[a-zA-Z0-9]+$/'; // 定义正则表达式模式，只包含字母和数字
        if (preg_match($pattern, $password)) {
            return true;
        }
        return false;
    }

    /**
     * 验证场景
     */
    protected $scene = [

    ];
}
